/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.device.modular.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.device.core.enums.Constants;
import vip.xiaonuo.device.modular.entity.ApiLog;
import vip.xiaonuo.device.modular.mapper.GoodsMapper;
import vip.xiaonuo.device.vo.ReagentWeighVO;
import vip.xiaonuo.device.vo.Result;

import java.math.BigDecimal;

/**
 * 试剂称重
 *
 * @author Jonathan
 * @date 2023/04/21 13:27
 **/
@Service("deviceReagentWeighService")
public class DeviceReagentWeighServiceImpl extends DeviceBaseService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    protected Integer getReqType() {
        return Constants.REAGENTWEIGH;
    }

    @Override
    protected Result handle(JSONObject param) {
        JSONObject data = param.getJSONObject("data");
        String reagentRfid = data.getString("reagentRfid");
        String currentWeight = data.getString("currentWeight");
        if (StringUtils.isBlank(reagentRfid) || StringUtils.isBlank(currentWeight)) {
            return Result.fail("参数缺失,请传入正确的参数值reagentRfid+currentWeight");
        }
        ReagentWeighVO reagentWeighVO = goodsMapper.getReagentByRfid(reagentRfid);
        if (reagentWeighVO == null) {
            return Result.fail("未知试剂");
        } else {
            String state = reagentWeighVO.getReagentStat();
            if (state.equals("0")) {
                reagentWeighVO.setReagentStat("已报废");
            }
            if (state.equals("1")) {
                reagentWeighVO.setReagentStat("正常");
            }
            if (state.equals("2")) {
                reagentWeighVO.setReagentStat("维修中");
            }
            if (StringUtils.isBlank(reagentWeighVO.getReagentFirstWeighApiId())) {
                reagentWeighVO.setWeighFlag(false);
            } else {
                reagentWeighVO.setWeighFlag(true);
            }
            return Result.ok(reagentWeighVO);
        }
    }

    @Override
    protected void handleLog(ApiLog log, JSONObject param) {
        JSONObject data = param.getJSONObject("data");
        String reagentRfid = data.getString("reagentRfid");
        String currentWeight = data.getString("currentWeight");
        log.setRfids(reagentRfid);
        log.setReagentWeight(new BigDecimal(currentWeight).abs());
    }
}
